/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package src;

import java.awt.Color;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 *Για validation JTextField με μέγιστο πλήθος χαρακτήρων
 * 
 */
public class TextMaxLengthTrimInputVerifier  extends InputVerifier{
    private final JLabel validationLabel;
    private final int maxLength;
    public TextMaxLengthTrimInputVerifier(JLabel validationLabel, int maxInputLength) {
        super();
        this.validationLabel = validationLabel;
        this.maxLength = maxInputLength;
    }
    
    @Override
    public boolean verify(JComponent input) {
        String trimed =  ((JTextField) input).getText().trim();
        ((JTextField) input).setText(trimed);
        boolean result = ((JTextField) input).getText().length() <= this.maxLength;
        if (!result)
            this.validationLabel.setForeground(Color.red);
        else
            this.validationLabel.setForeground(Color.black);
        return result;
    }

    
    
    
}
